package com.sanley.coronavirus.service.iml;

import com.github.pagehelper.PageHelper;
import com.sanley.coronavirus.dao.BaseDao;
import com.sanley.coronavirus.dao.PatientDao;
import com.sanley.coronavirus.dao.TouchDao;
import com.sanley.coronavirus.entity.Patient;
import com.sanley.coronavirus.service.PatientService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.sql.Date;
import java.util.List;

@Service
public class PatientServiceImp implements PatientService {

    private Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private PatientDao patientDao;

    @Autowired
    private BaseDao baseDao;

    @Autowired
    private TouchDao touchDao;

    @Override
    public List<Patient> findAll(int page, int size) {
        PageHelper.startPage(page, size);
        return patientDao.findAll();
    }

    @Override
    @Transactional
    public boolean add(Patient patient) {

        try {
            //更新密接状态
            touchDao.toSafe(patient.getBaseId());
            //添加到确诊表
            patientDao.add(patient);
            return true;
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    public void update(Patient patient) {
        patientDao.update(patient);
    }

    @Override
    public List<Patient> findByName(String name) {
        return patientDao.findByName("%" + name + "%");
    }

    @Override
    public Patient findById(int id) {
        return patientDao.findById(id);
    }

    @Override
    public int number() {
        return patientDao.number();
    }

    @Override
    public int currentNumber() {
        return patientDao.currentNumber();
    }


    @Override
    public int beforeDay(Date date) {
        return patientDao.beforeDay(date);
    }


}
